import { fetchJIRA } from '@/services/feedback';

export default {
    namespace: 'feedback',

    state: {
        processVisible: false,
        record: {},
        comments: []
    },

    reducers: {
        processVisible(state, { payload }) {
            return { ...state, processVisible: payload };
        },
        setRecord(state, { payload }) {
            return { ...state, record: payload };
        },
        setComments(state, { payload }) {
            return { ...state, comments: payload };
        }
    },

    effects: {
        *fetchProgress({ payload = {}, callback }, { put, call }) {
            yield put({
                type: 'setRecord',
                payload
            });

            const response = yield call(fetchJIRA, payload.jirano || '');
            const { data = [] } = response || {};
            const finalData = data.reverse();

            if (response && response.code === 10000) {
                yield put({
                    type: 'setComments',
                    payload: finalData
                });
            }

            if (callback) callback(response);
        }
    },

    subscriptions: {}
};
